//
//  VisitorView.swift
//  swiftweibo
//
//  Created by 吕建发 on 16/5/14.
//  Copyright © 2016年 cn.itcast. All rights reserved.
//

import UIKit
import SnapKit
 /// 代理
protocol VisitorViewDelegate: NSObjectProtocol {
    
    /// 访客视图注册
    func visitorViewRegist()
    
    /// 访客视图登录
    func vistorViewLogin()
}



class VisitorView: UIView {
    
    
     /// 代理属性
    weak var delegate: VisitorViewDelegate?
    
    
    /// Icon
    lazy var iconView: UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    
    /// 小房子
    lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///  遮罩
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
 
     /// label
    lazy var label = UILabel(gk_title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",alignment: .Center)
    
    
    lazy var registerButton = UIButton(gk_title: "注册", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor())
    
    lazy var loginButton = UIButton(gk_title: "登录", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor())
        
    
    
    ///   初始化方法
    ///
    ///   - parameter frame: frame description
    ///
    ///   - returns:
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
        // 给按钮增加单击方法
        registerButton.addTarget(self, action: #selector(VisitorView.didClickRegisterButton), forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: #selector(VisitorView.didClickLoginButton), forControlEvents: .TouchUpInside)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK: - 登录和注册按钮的单击方法
    @objc private func didClickRegisterButton() {
        
        print("点击了注册")
        delegate?.visitorViewRegist()
        
    }
    
    @objc private func didClickLoginButton() {
        print("点击了登录")
        
        delegate?.vistorViewLogin()
    }
    
}

extension VisitorView {
    ///   给外部提供修改图片的方法
    ///
    ///   - returns:
    func setUpSubView(imageName: String? = nil, messageTitle: String) {
        
        label.text = messageTitle
        
        guard let imageName = imageName else{
            
            startAnmation()
            
            return
        }
        
        coverView.hidden = true
        iconView.hidden = true
        houseView.image = UIImage(named: imageName)
    }
    
    ///   设置动画
    private func startAnmation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        // 提示：对于需要循环播放的动画，可以设置此属性为 false
        // 视图离开屏幕后，不会被销毁！当 动画所在视图被销毁后，动画会连带销毁
        // 不需要考虑释放问题
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    
}



// MARK: - 添加访客视图中的子控件
extension VisitorView {
    
    ///   设置UI控件
   private func setUpUI() {
        addSubview(iconView)
        addSubview(houseView)
        addSubview(coverView)
        addSubview(label)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 自动布局
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        houseView.snp_makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        label.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom)
            make.centerX.equalTo(iconView.snp_centerX)
            make.size.equalTo(CGSize(width: 224, height: 36))
            
            // make.width.equalTo(iconView)
        }
        
        // 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(label.snp_bottom).offset(20)
            make.left.equalTo(label)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        // 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(label)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        // 遮罩视图
        coverView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
     
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        
    }
    
    
    
    
    
    
}

